<?php

// MODELS
include_once "application/models/Member.php";

/**
 * Authentification class
 */
class Authentification {

    /**
     * Connecte l'utilisateur si les informations login et mot de passe saisies sont correctes
     * @return type
     */
    public static function doConnect() {

        if (Authentification::isAuthenticated())
            session_unset();

        if (isset($_POST['username']) && isset($_POST['password'])) {
            $login = $_POST['username'];
            $password = $_POST['password'];
            $password = md5($password);

            $userId = Member::getIdAuthentify($login, $password);

            if ($userId != -1) {
                $member = Member::getMemberById($userId);
                Authentification::saveMember($member);
            }
        }
        return;
    }

    /**
     * Sauvegarde le membre en session
     * @param type $member
     */
    private static function saveMember($member) {
        $_SESSION['id_member'] = $member->getId();
        $_SESSION['login'] = $member->getLogin();
        $_SESSION['email'] = $member->getEmail();
        $_SESSION['first_name'] = $member->getFirst_name();
        $_SESSION['last_name'] = $member->getLast_name();
        $_SESSION['address'] = $member->getAddress();
        $_SESSION['company'] = $member->getCompany();
        $_SESSION['city'] = $member->getCity();
        $_SESSION['zip'] = $member->getZip();
        $_SESSION['phone_number'] = $member->getPhone_number();
        $_SESSION['avatar'] = $member->getAvatar();
        $_SESSION['statut_member'] = $member->getStatut();
        $_SESSION['is_enabled'] = $member->getIs_enabled();
    }

    /**
     * Récupère l'utilisateur connecter
     * @return \Member|null
     */
    public static function getCurrentMember() {
        if (Authentification::isAutenticated())
            return new Member($_SESSION['id_member'], $_SESSION['login'], null, $_SESSION['email'], $_SESSION['first_name'], $_SESSION['last_name'], $_SESSION['address'], $_SESSION['company'], $_SESSION['city'], $_SESSION['zip'], $_SESSION['phone_number'], $_SESSION['avatar'], $_SESSION['statut'], $_SESSION['is_enabled']);
        else
            return null;
    }

    /**
     * Récupère l'id du membre connecté
     * @return type
     */
    public static function getCurrentMemberId() {
        if (Authentification::isAuthenticated())
            return $_SESSION['id_member'];
    }

    /**
     * Récupère le statut du membre connecté
     * @return type
     */
    public static function getCurrentMemberStatut() {
        if (Authentification::isAuthenticated())
            return $_SESSION['statut_member'];
    }

    /**
     * Récupère le nom du membre connecté
     * @return type
     */
    public static function getCurrentMemberName() {
        if (Authentification::isAuthenticated())
            return $_SESSION['first_name'] . ' ' . $_SESSION['last_name'];
    }

    /**
     * Retourne si l'utilisateur est connecté ou non
     * @return type
     */
    static public function isAuthenticated() {
        return isset($_SESSION['id_member']);
    }

    /**
     * Affiche un projet par défaut
     */
    static public function setCurrentProjectId() {
        if (Authentification::isAuthenticated() && isset($_GET['project']) && ($_GET['project'] != null)) {
            $_SESSION['id_project'] = $_GET['project'];
        }
    }

    /**
     * Retourne l'id du projet courant
     * @return type
     */
    static public function getCurrentProjectId() {
        if (isset($_SESSION['id_project']))
            return $_SESSION['id_project'];
    }

    /**
     * Détermine si l'utilisateur peut accéder à la partie administration
     * @return boolean
     */
    static public function canAdminAcces() {

        if (Authentification::getCurrentMemberStatut() == Member::FULLADMIN)
            return true;
        $projects = Project::getProjectsOfMember(Authentification::getCurrentMemberId(), Member::USER);
        foreach ($projects as $p) {
            if ($p->getUserType() == Project::ADMIN)
                return true;
        }
        return false;
    }

    /**
     * Détermine si l'utilisateur peut accéder à la partie projet
     * @return boolean
     */
    static public function canProjectAcces() {
        return true;
    }

    /**
     * 
     * Déconnecte l'utilsateur
     * 
     */
    static public function doDeconnect() {
        if ((isset($_GET['action'])) && ($_GET['action'] == 'logOut')) {
            session_unset();
            session_destroy();
            header('Location: index.php');
        }
    }

}